<template>
	<view style="padding: 40rpx;">
		<view class="img">
			<image class="img_src" src="../../static/logo.png" mode=""></image>
		</view>
		<view class="content_explain">
			<view style="font-size: 50rpx;margin-bottom: 40rpx;">账号密码登录</view>
			<text>欢迎使用智慧环保app</text>
			<u-radio-group activeColor="#007aff" placement="row" v-model="rediovalue">
				<u-radio style="margin-right: 20rpx;" label="个人用户" name="个人用户"></u-radio>
				<u-radio label="企业用户" name="企业用户"></u-radio>
			</u-radio-group>
		</view>
		<view class="content_realization">
			手机号码
			<u-input border="none" class="realization_input" placeholder="请输入手机号" v-model="user.phone">
				<u--text text="+86 |" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
			</u-input>
			密码
			<u-input :password="true" border="none" class="realization_input" placeholder="请输入密码"
				v-model="user.password">
			</u-input>
			<view style="display: flex;justify-content: space-between;">
				<view style="display: flex;justify-content: flex-start;">
					<u-checkbox-group placement="row">
						<u-checkbox @change="checkbox" activeColor="#2778ff" labelSize="10" label="我已阅读并同意">
						</u-checkbox>
					</u-checkbox-group>
					<text style="font-size: 10px;color: #007AFF;">《用户隐私政策》</text>
				</view>
				<view @click="toForgotPassword" style="font-size: 10px;color: #007AFF;">忘记密码？</view>
			</view>
		</view>
		<view style="margin-bottom: 20rpx;">
			<button type="primary" @click="login">登录</button>
		</view>
		<view style="display: flex;justify-content: space-between;margin-bottom: 100rpx;">
			<text @click="toLogin">手机号验证登录</text>
			<text @click="toRegister">注册新用户</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '获取验证码',
				checkboxChecked: false,
				rediovalue: '个人用户',
				user: {},
				user_:{}
			}
		},
		methods: {
			toForgotPassword() {
				// 忘记密码
				uni.navigateTo({
					url: "./forgotPassword?a="+(this.rediovalue=="个人用户"?"0":"1")
				})
			},
			checkbox() {
				this.checkboxChecked = !this.checkboxChecked
				console.log(this.checkboxChecked);
			},
			login() {
				if (this.user.phone.length == 0) {
					uni.showToast({
						title: '请输入账号！',
						icon: 'none'
					})
				} else if (this.user.password.length == 0) {
					uni.showToast({
						title: '请输入密码！',
						icon: 'none'
					})
				} else if (!this.checkboxChecked) {
					uni.showToast({
						title: '请同意用户隐私政策',
						icon: 'none'
					})
				} else {
					if (this.rediovalue == '企业用户') {
						// 调用企业用户登录接口
						this.$api.eUserLogin(this.user).then(res => {
							console.log(res);
							this.user_=res.data
							this.user_.a=1
							if (res.code == 200) {
								// 登录成功
								uni.setStorage({
									key:'user',
									data:this.user_
								})
								uni.navigateTo({
									url: '../enterpriseSide/enterpriseSide'
								})
							} else {
								uni.$u.toast(res.msg)
							}
						})

					} else {
						// 调用普通用户登录接口
						this.$api.iUserLogin(this.user).then(res => {
							console.log(res);
							this.user_=res.data
							this.user_.a=0
							if (res.code == 200) {
								// 登录成功
								uni.setStorage({
									key:'user',
									data:this.user_
								})
								uni.switchTab({
									url: "../home/home"
								})
							} else {
								uni.$u.toast(res.msg)
							}
						})
					}

				}
			},
			toRegister() {
				// 跳转到注册页面
				uni.navigateTo({
					url: "../register/register"
				})
			},
			toLogin() {
				// 跳转到用户名密码登录界面
				uni.navigateTo({
					url: "./login"
				})

			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			change(e) {
				console.log('change', e);
			}
		}
	}
</script>

<style>
	.realization_input {
		background-color: #f2f7fb;
		height: 60rpx;
	}

	.img {
		width: 100%;
		text-align: center;
		margin: 50rpx 0;
	}

	.img_src {
		height: 200rpx;
		width: 200rpx;
	}

	.content_explain {
		margin-bottom: 40rpx;
	}

	.content_realization {
		line-height: 80rpx;
	}
</style>
